home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / getpass.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  5KB  |  181 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''Utilities to get a password and/or the current user name.
  5.  
  6. getpass(prompt[, stream]) - Prompt for a password, with echo turned off.
  7. getuser() - Get the user name from the environment or password database.
  8.  
  9. GetPassWarning - This UserWarning is issued when getpass() cannot prevent
  10.                  echoing of the password contents while reading.
  11.  
  12. On Windows, the msvcrt module will be used.
  13. On the Mac EasyDialogs.AskPassword is used, if available.
  14.  
  15. '''
  16. import os
  17. import sys
  18. import warnings
  19. __all__ = [
  20.     'getpass',
  21.     'getuser',
  22.     'GetPassWarning']
  23.  
  24. class GetPassWarning(UserWarning):
  25.     pass
  26.  
  27.  
  28. def unix_getpass(prompt = 'Password: ', stream = None):
  29.     """Prompt for a password, with echo turned off.
  30.  
  31.     Args:
  32.       prompt: Written on stream to ask for the input.  Default: 'Password: '
  33.       stream: A writable file object to display the prompt.  Defaults to
  34.               the tty.  If no tty is available defaults to sys.stderr.
  35.     Returns:
  36.       The seKr3t input.
  37.     Raises:
  38.       EOFError: If our input tty or stdin was closed.
  39.       GetPassWarning: When we were unable to turn echo off on the input.
  40.  
  41.     Always restores terminal settings before returning.
  42.     """
  43.     fd = None
  44.     tty = None
  45.     
  46.     try:
  47.         fd = os.open('/dev/tty', os.O_RDWR | os.O_NOCTTY)
  48.         tty = os.fdopen(fd, 'w+', 1)
  49.         input = tty
  50.         if not stream:
  51.             stream = tty
  52.     except EnvironmentError:
  53.         e = None
  54.         
  55.         try:
  56.             fd = sys.stdin.fileno()
  57.         except (AttributeError, ValueError):
  58.             passwd = fallback_getpass(prompt, stream)
  59.  
  60.         input = sys.stdin
  61.         if not stream:
  62.             stream = sys.stderr
  63.         
  64.  
  65.     if fd is not None:
  66.         passwd = None
  67.         
  68.         try:
  69.             old = termios.tcgetattr(fd)
  70.             new = old[:]
  71.             new[3] &= ~(termios.ECHO)
  72.             tcsetattr_flags = termios.TCSAFLUSH
  73.             if hasattr(termios, 'TCSASOFT'):
  74.                 tcsetattr_flags |= termios.TCSASOFT
  75.             
  76.             try:
  77.                 termios.tcsetattr(fd, tcsetattr_flags, new)
  78.                 passwd = _raw_input(prompt, stream, input = input)
  79.             finally:
  80.                 termios.tcsetattr(fd, tcsetattr_flags, old)
  81.                 stream.flush()
  82.  
  83.         except termios.error:
  84.             e = None
  85.             if passwd is not None:
  86.                 raise 
  87.             del input
  88.             del tty
  89.             passwd = fallback_getpass(prompt, stream)
  90.         
  91.  
  92.     stream.write('\n')
  93.     return passwd
  94.  
  95.  
  96. def win_getpass(prompt = 'Password: ', stream = None):
  97.     '''Prompt for password with echo off, using Windows getch().'''
  98.     if sys.stdin is not sys.__stdin__:
  99.         return fallback_getpass(prompt, stream)
  100.     import msvcrt as msvcrt
  101.     for c in prompt:
  102.         msvcrt.putch(c)
  103.     
  104.     pw = ''
  105.     while None:
  106.         c = msvcrt.getch()
  107.         if c == '\r' or c == '\n':
  108.             break
  109.         if c == '\x03':
  110.             raise KeyboardInterrupt
  111.         if c == '\x08':
  112.             pw = pw[:-1]
  113.             continue
  114.         pw = pw + c
  115.         continue
  116.         msvcrt.putch('\n')
  117.         return pw
  118.  
  119.  
  120. def fallback_getpass(prompt = 'Password: ', stream = None):
  121.     warnings.warn('Can not control echo on the terminal.', GetPassWarning, stacklevel = 2)
  122.     if not stream:
  123.         stream = sys.stderr
  124.     print >>stream, 'Warning: Password input may be echoed.'
  125.     return _raw_input(prompt, stream)
  126.  
  127.  
  128. def _raw_input(prompt = '', stream = None, input = None):
  129.     if not stream:
  130.         stream = sys.stderr
  131.     if not input:
  132.         input = sys.stdin
  133.     prompt = str(prompt)
  134.     if prompt:
  135.         stream.write(prompt)
  136.         stream.flush()
  137.     line = input.readline()
  138.     if not line:
  139.         raise EOFError
  140.     if line[-1] == '\n':
  141.         line = line[:-1]
  142.     return line
  143.  
  144.  
  145. def getuser():
  146.     '''Get the username from the environment or password database.
  147.  
  148.     First try various environment variables, then the password
  149.     database.  This works on Windows as long as USERNAME is set.
  150.  
  151.     '''
  152.     import os
  153.     for name in ('LOGNAME', 'USER', 'LNAME', 'USERNAME'):
  154.         user = os.environ.get(name)
  155.         if user:
  156.             return user
  157.     
  158.     import pwd as pwd
  159.     return pwd.getpwuid(os.getuid())[0]
  160.  
  161.  
  162. try:
  163.     import termios
  164.     (termios.tcgetattr, termios.tcsetattr)
  165. except (ImportError, AttributeError):
  166.     
  167.     try:
  168.         import msvcrt
  169.     except ImportError:
  170.         
  171.         try:
  172.             from EasyDialogs import AskPassword
  173.         except ImportError:
  174.             getpass = fallback_getpass
  175.  
  176.         getpass = AskPassword
  177.  
  178.     getpass = win_getpass
  179.  
  180. getpass = unix_getpass
  181.